package com.seeyon.apps.common.utils;


import com.seeyon.cap4.form.api.FormApi4Cap4;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormDataSubBean;
import com.seeyon.cap4.form.bean.FormFieldBean;
import com.seeyon.cap4.form.bean.FormFieldComEnum;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumItem;
import com.seeyon.ctp.organization.bo.V3xOrgAccount;
import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.bo.V3xOrgLevel;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.bo.V3xOrgPost;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.organization.po.OrgLevel;
import com.seeyon.ctp.organization.po.OrgMember;
import com.seeyon.ctp.organization.po.OrgPost;
import com.seeyon.ctp.organization.po.OrgUnit;
import com.seeyon.ctp.util.Strings;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Project: 常用工具类
 * @Package: com.seeyon.apps.common.utils
 * @Author: CheungHowrAn
 * @CreateTime: 2022-09-23  16:53
 * @Description: CAP4表单取值工具类
 * @Version: 1.0
 */
public class CAP4FormFieldUtils {

    private static final Log LOGGER = CtpLogFactory.getLog(CAP4FormFieldUtils.class);
    private static FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
    private static OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
    private static EnumManager enumManager = (EnumManager) AppContext.getBean("enumManagerNew");

    /**
     * @MethodName getFormDataMasterBeanByTableName
     * @Description 根据明细表数据库表名&明细表ID获取主表单数据
     * @param: formCode
     * @param: subDataId
     * @return: com.seeyon.cap4.form.bean.FormDataSubBean
     * @Author CheungHowrAn
     * @Date 2024/12/14 23:48
     */
    public FormDataMasterBean getFormDataMasterBeanByTableName(String tableName, Long subDataId) throws Exception {
        FormTableBean tableBean = formApi4Cap4.getFormTableBeanByName(tableName);
        if (tableBean != null) {
            Long formId = tableBean.getFormId();
            FormBean formBean = formApi4Cap4.getForm(formId);
            StringBuffer sql = new StringBuffer();
            sql.append("select formmain_id from ").append(tableName).append(" where id = ").append(subDataId);
            List<Map<String, Object>> list = JDBCAgentUtils.query(sql.toString());
            if (list != null && list.size() > 0) {
                Long formMainId = Long.valueOf(list.get(0).get("formmain_id").toString());
                FormDataMasterBean formDataMasterBean = formApi4Cap4.getDataMasterBeanById(formMainId, formBean, null);
                return formDataMasterBean;
            }
        }
        return null;
    }

    /**
     * @MethodName getFormDataMasterBeanByFormCode
     * @Description 根据主表单编码&明细表ID获取主表单数据
     * @param: formCode
     * @param: subDataId
     * @return: com.seeyon.cap4.form.bean.FormDataSubBean
     * @Author CheungHowrAn
     * @Date 2024/12/14 23:48
     */
    public FormDataMasterBean getFormDataMasterBeanByFormCode(String formCode, Long subDataId) throws Exception {
        FormBean formBean = formApi4Cap4.getFormByFormCode(formCode);
        if (formBean != null) {
            List<FormTableBean> subTableBean = formBean.getSubTableBean();
            for (FormTableBean formTableBean : subTableBean) {
                String tableName = formTableBean.getTableName();
                StringBuffer sql = new StringBuffer();
                sql.append("select formmain_id from ").append(tableName).append(" where id = ").append(subDataId);
                List<Map<String, Object>> list = JDBCAgentUtils.query(sql.toString());
                if (list != null && list.size() > 0) {
                    Long formMainId = Long.valueOf(list.get(0).get("formmain_id").toString());
                    FormDataMasterBean formDataMasterBean = formApi4Cap4.getDataMasterBeanById(formMainId, formBean, null);
                    return formDataMasterBean;
                }
            }
        }
        return null;
    }

    /**
     * @MethodName getSubDataBean
     * @Description 根据明细表数据库表名&明细表ID获取明细单数据
     * @param: formCode
     * @param: subDataId
     * @return: com.seeyon.cap4.form.bean.FormDataSubBean
     * @Author CheungHowrAn
     * @Date 2024/12/14 23:48
     */
    public FormDataSubBean getSubDataBeanByTableName(String tableName, Long subDataId) throws Exception {
        FormTableBean tableBean = formApi4Cap4.getFormTableBeanByName(tableName);
        if (tableBean != null) {
            Long formId = tableBean.getFormId();
            FormBean formBean = formApi4Cap4.getForm(formId);
            StringBuffer sql = new StringBuffer();
            sql.append("select formmain_id from ").append(tableName).append(" where id = ").append(subDataId);
            List<Map<String, Object>> list = JDBCAgentUtils.query(sql.toString());
            if (list != null && list.size() > 0) {
                Long formMainId = Long.valueOf(list.get(0).get("formmain_id").toString());
                FormDataMasterBean formDataMasterBean = formApi4Cap4.getDataMasterBeanById(formMainId, formBean, null);
                List<FormDataSubBean> subData = formDataMasterBean.getSubData(tableName);
                for (FormDataSubBean subDatum : subData) {
                    if (subDatum.getId().equals(subDataId)) {
                        return subDatum;
                    }
                }
            }
        }
        return null;
    }

    /**
     * @MethodName getSubDataBean
     * @Description 根据主表单编码&明细表ID获取明细单数据
     * @param: formCode
     * @param: subDataId
     * @return: com.seeyon.cap4.form.bean.FormDataSubBean
     * @Author CheungHowrAn
     * @Date 2024/12/14 23:48
     */
    public FormDataSubBean getSubDataBeanByFormCode(String formCode, Long subDataId) throws Exception {
        FormBean formBean = formApi4Cap4.getFormByFormCode(formCode);
        if (formBean != null) {
            List<FormTableBean> subTableBean = formBean.getSubTableBean();
            for (FormTableBean formTableBean : subTableBean) {
                String tableName = formTableBean.getTableName();
                StringBuffer sql = new StringBuffer();
                sql.append("select formmain_id from ").append(tableName).append(" where id = ").append(subDataId);
                List<Map<String, Object>> list = JDBCAgentUtils.query(sql.toString());
                if (list != null && list.size() > 0) {
                    Long formMainId = Long.valueOf(list.get(0).get("formmain_id").toString());
                    FormDataMasterBean formDataMasterBean = formApi4Cap4.getDataMasterBeanById(formMainId, formBean, null);
                    List<FormDataSubBean> subData = formDataMasterBean.getSubData(tableName);
                    for (FormDataSubBean subDatum : subData) {
                        if (subDatum.getId().equals(subDataId)) {
                            return subDatum;
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     * @Description: 获取表单的值
     * @Author: CheungHowrAn
     * @Date: 2023-4-24
     * @Param: [bean, disPlay, type]
     * @return: java.lang.String
     */
    public static String getFormFieldVal(FormDataBean bean, String disPlay, FormFieldValType type) throws Exception {
        String val = "";
        if (getFieldValue(bean, disPlay) != null) {
            FormFieldBean fieldBean = getFieldBean(bean, disPlay);
            FormFieldComEnum inputTypeEnum = fieldBean.getInputTypeEnum();
            switch (inputTypeEnum) {
                case SELECT:
                case RADIO:
                    Long enumId = DataConvertUtils.toLong(getFieldValue(bean, disPlay));
                    CtpEnumItem enumItem = enumManager.getEnumItem(enumId);
                    if (type.equals(FormFieldValType.SHOW_CODE)) {
                        if (StringUtils.isNotBlank(enumItem.getEnumItemCode())) {
                            val = enumItem.getEnumItemCode();
                        } else {
                            val = enumItem.getEnumvalue();
                        }
                    } else if (type.equals(FormFieldValType.SHOW_SORT)) {
                        val = enumItem.getSortnumber().toString();
                    } else if (type.equals(FormFieldValType.SHOW_NAME)) {
                        val = enumItem.getShowvalue();
                    } else if (type.equals(FormFieldValType.ID)) {
                        val = enumItem.getId().toString();
                    }
                    break;
                case EXTEND_DATE:
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    val = dateFormat.format(DataConvertUtils.toDate(getFieldValue(bean, disPlay)));
                    break;
                case EXTEND_DATETIME:
                    SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    val = dateTimeFormat.format(DataConvertUtils.toDate(getFieldValue(bean, disPlay)));
                    break;
                case EXTEND_MEMBER:
                    V3xOrgMember member = orgManager.getMemberById(DataConvertUtils.toLong(getFieldValue(bean, disPlay)));
                    if (type.equals(FormFieldValType.SHOW_CODE)) {
                        val = member.getCode();
                    } else if (type.equals(FormFieldValType.SHOW_NAME)) {
                        val = member.getName();
                    } else if (type.equals(FormFieldValType.LOGIN_NAME)) {
                        val = member.getLoginName();
                    } else if (type.equals(FormFieldValType.ID)) {
                        val = DataConvertUtils.toStr(getFieldValue(bean, disPlay));
                    }
                    break;
                case EXTEND_MULTI_MEMBER:
                    String memberIds = DataConvertUtils.toStr(getFieldValue(bean, disPlay));
                    for (String id : memberIds.split(",")) {
                        V3xOrgMember v3xOrgMember = orgManager.getMemberById(DataConvertUtils.toLong(id));
                        if (type.equals(FormFieldValType.SHOW_CODE)) {
                            val += v3xOrgMember.getCode() + ",";
                        } else if (type.equals(FormFieldValType.SHOW_NAME)) {
                            val += v3xOrgMember.getName() + ",";
                        } else if (type.equals(FormFieldValType.LOGIN_NAME)) {
                            val += v3xOrgMember.getLoginName() + ",";
                        } else if (type.equals(FormFieldValType.ID)) {
                            val = DataConvertUtils.toStr(getFieldValue(bean, disPlay)) + ",";
                        }
                    }
                    val = StringUtils.removeEnd(val, ",");
                    break;
                case EXTEND_ACCOUNT:
                    V3xOrgAccount account = orgManager.getAccountById(DataConvertUtils.toLong(getFieldValue(bean, disPlay)));
                    if (type.equals(FormFieldValType.SHOW_CODE)) {
                        val = account.getCode();
                    } else if (type.equals(FormFieldValType.SHOW_NAME)) {
                        val = account.getName();
                    } else if (type.equals(FormFieldValType.ID)) {
                        val = DataConvertUtils.toStr(getFieldValue(bean, disPlay));
                    }
                    break;
                case EXTEND_MULTI_ACCOUNT:
                    String accountIds = DataConvertUtils.toStr(getFieldValue(bean, disPlay));
                    for (String id : accountIds.split(",")) {
                        V3xOrgAccount v3xOrgAccount = orgManager.getAccountById(DataConvertUtils.toLong(id));
                        if (type.equals(FormFieldValType.SHOW_CODE)) {
                            val += v3xOrgAccount.getCode() + ",";
                        } else if (type.equals(FormFieldValType.SHOW_NAME)) {
                            val += v3xOrgAccount.getName() + ",";
                        } else if (type.equals(FormFieldValType.ID)) {
                            val = DataConvertUtils.toStr(getFieldValue(bean, disPlay)) + ",";
                        }
                    }
                    val = StringUtils.removeEnd(val, ",");
                    break;
                case EXTEND_DEPARTMENT:
                    V3xOrgDepartment department = orgManager.getDepartmentById(DataConvertUtils.toLong(getFieldValue(bean, disPlay)));
                    if (type.equals(FormFieldValType.SHOW_CODE)) {
                        val = department.getCode();
                    } else if (type.equals(FormFieldValType.SHOW_NAME)) {
                        val = department.getName();
                    } else if (type.equals(FormFieldValType.ID)) {
                        val = DataConvertUtils.toStr(getFieldValue(bean, disPlay));
                    }
                    break;
                case EXTEND_MULTI_DEPARTMENT:
                    String departmentIds = DataConvertUtils.toStr(getFieldValue(bean, disPlay));
                    for (String id : departmentIds.split(",")) {
                        V3xOrgDepartment v3xOrgDepartment = orgManager.getDepartmentById(DataConvertUtils.toLong(id));
                        if (type.equals(FormFieldValType.SHOW_CODE)) {
                            val += v3xOrgDepartment.getCode() + ",";
                        } else if (type.equals(FormFieldValType.SHOW_NAME)) {
                            val += v3xOrgDepartment.getName() + ",";
                        } else if (type.equals(FormFieldValType.ID)) {
                            val = DataConvertUtils.toStr(getFieldValue(bean, disPlay)) + ",";
                        }
                    }
                    val = StringUtils.removeEnd(val, ",");
                    break;
                case EXTEND_POST:
                    V3xOrgPost post = orgManager.getPostById(DataConvertUtils.toLong(getFieldValue(bean, disPlay)));
                    if (type.equals(FormFieldValType.SHOW_CODE)) {
                        val = post.getCode();
                    } else if (type.equals(FormFieldValType.SHOW_NAME)) {
                        val = post.getName();
                    } else if (type.equals(FormFieldValType.ID)) {
                        val = DataConvertUtils.toStr(getFieldValue(bean, disPlay));
                    }
                    break;
                case EXTEND_MULTI_POST:
                    String postIds = DataConvertUtils.toStr(getFieldValue(bean, disPlay));
                    for (String id : postIds.split(",")) {
                        V3xOrgPost v3xOrgPost = orgManager.getPostById(DataConvertUtils.toLong(id));
                        if (type.equals(FormFieldValType.SHOW_CODE)) {
                            val += v3xOrgPost.getCode() + ",";
                        } else if (type.equals(FormFieldValType.SHOW_NAME)) {
                            val += v3xOrgPost.getName() + ",";
                        } else if (type.equals(FormFieldValType.ID)) {
                            val = DataConvertUtils.toStr(getFieldValue(bean, disPlay)) + ",";
                        }
                    }
                    val = StringUtils.removeEnd(val, ",");
                    break;
                case EXTEND_LEVEL:
                    V3xOrgLevel level = orgManager.getLevelById(DataConvertUtils.toLong(getFieldValue(bean, disPlay)));
                    if (type.equals(FormFieldValType.SHOW_CODE)) {
                        val = level.getCode();
                    } else if (type.equals(FormFieldValType.SHOW_NAME)) {
                        val = level.getName();
                    } else if (type.equals(FormFieldValType.ID)) {
                        val = DataConvertUtils.toStr(getFieldValue(bean, disPlay));
                    }
                    break;
                case EXTEND_MULTI_LEVEL:
                    String levelIds = DataConvertUtils.toStr(getFieldValue(bean, disPlay));
                    for (String id : levelIds.split(",")) {
                        V3xOrgLevel v3xOrgLevel = orgManager.getLevelById(DataConvertUtils.toLong(id));
                        if (type.equals(FormFieldValType.SHOW_CODE)) {
                            val += v3xOrgLevel.getCode() + ",";
                        } else if (type.equals(FormFieldValType.SHOW_NAME)) {
                            val += v3xOrgLevel.getName() + ",";
                        } else if (type.equals(FormFieldValType.ID)) {
                            val = DataConvertUtils.toStr(getFieldValue(bean, disPlay)) + ",";
                        }
                    }
                    break;

                default:
                    val = DataConvertUtils.toStr(getFieldValue(bean, disPlay));
            }

        }
        return val;
    }

    /**
     * @Description: 根据传的值跟值的类型 获取表单中应存的值
     * @Author: CheungHowrAn
     * @Date: 2023-4-24
     * @Param: [bean, val]
     * @return: java.lang.String
     */
    public static String getFormFieldOriginalVal(FormFieldBean bean, String val, FormFieldValType type) throws BusinessException {
        String originalVal = "";
        if (Strings.isNotBlank(val)) {
            FormFieldComEnum inputTypeEnum = bean.getInputTypeEnum();
            switch (inputTypeEnum) {
                case CHECKBOX:
                    originalVal = "0";
                    if (type.equals(FormFieldValType.SHOW_NAME)) {
                        if (StringUtils.isNotEmpty(val)) {
                            List<String> checkList = Arrays.asList(val.split(","));
                            if (checkList.contains(bean.getDisplay())) {
                                originalVal = "1";
                            }
                        }
                    }
                    break;
                case SELECT:
                case RADIO:
                    originalVal = val;
                    long enumId = bean.getEnumId();
                    if (type.equals(FormFieldValType.SHOW_NAME)) {
                        CtpEnumItem enumItem = enumManager.getItemByEnumId(enumId, val);
                        originalVal = enumItem.getId().toString();
                    }
                    break;
                case EXTEND_MEMBER:
                    originalVal = val;
                    if (type.equals(FormFieldValType.SHOW_NAME)) {
                        List<OrgMember> memberList = OrgDataUtils.findMemberByName(null, val);
                        if (memberList.size() > 0) {
                            originalVal = memberList.get(0).getId().toString();
                        }
                    } else if (type.equals(FormFieldValType.LOGIN_NAME)) {
                        V3xOrgMember member = orgManager.getMemberByLoginName(val);
                        if (member != null) {
                            originalVal = member.getId().toString();
                        } else if (type.equals(FormFieldValType.SHOW_CODE)) {
                            List<OrgMember> memberList = OrgDataUtils.findMemberByCode(null, val);
                            if (memberList.size() > 0) {
                                originalVal = memberList.get(0).getId().toString();
                            }
                        }
                    }
                    break;
                case EXTEND_MULTI_MEMBER:
                    originalVal = val;
                    if (type.equals(FormFieldValType.SHOW_NAME)) {
                        String newVal = "";
                        for (String name : val.split(",")) {
                            List<OrgMember> memberList = OrgDataUtils.findMemberByName(null, name);
                            if (memberList.size() > 0) {
                                newVal += memberList.get(0).getId().toString();
                            }
                        }
                        originalVal = newVal;
                    } else if (type.equals(FormFieldValType.SHOW_CODE)) {
                        String newVal = "";
                        for (String loginName : val.split(",")) {
                            V3xOrgMember member = orgManager.getMemberByLoginName(loginName);
                            if (member != null) {
                                newVal += member.getId().toString();
                            }
                        }
                        originalVal = newVal;
                    } else if (type.equals(FormFieldValType.SHOW_CODE)) {
                        String newVal = "";
                        for (String code : val.split(",")) {
                            List<OrgMember> memberList = OrgDataUtils.findMemberByCode(null, code);
                            if (memberList.size() > 0) {
                                newVal += memberList.get(0).getId().toString();
                            }
                        }
                        originalVal = newVal;
                    }
                    originalVal = StringUtils.removeEnd(originalVal, ",");
                    break;
                case EXTEND_ACCOUNT:
                    originalVal = val;
                    if (type.equals(FormFieldValType.SHOW_NAME)) {
                        List<OrgUnit> deptList = OrgDataUtils.findAccountByName(val);
                        if (Strings.isNotEmpty(deptList)) {
                            originalVal = deptList.get(0).getId().toString();
                        }
                    } else if (type.equals(FormFieldValType.SHOW_CODE)) {
                        List<OrgUnit> deptList = OrgDataUtils.findAccountByCode(val);
                        if (Strings.isNotEmpty(deptList)) {
                            originalVal = deptList.get(0).getId().toString();
                        }
                    }
                    break;
                case EXTEND_MULTI_ACCOUNT:
                    originalVal = val;
                    if (type.equals(FormFieldValType.SHOW_NAME)) {
                        String newVal = "";
                        for (String name : val.split(",")) {
                            List<OrgUnit> deptList = OrgDataUtils.findAccountByName(name);
                            if (Strings.isNotEmpty(deptList)) {
                                newVal += deptList.get(0).getId().toString() + ",";
                            }
                        }
                        originalVal = newVal;
                    } else if (type.equals(FormFieldValType.SHOW_CODE)) {
                        String newVal = "";
                        for (String code : val.split(",")) {
                            List<OrgUnit> deptList = OrgDataUtils.findAccountByCode(code);
                            if (Strings.isNotEmpty(deptList)) {
                                newVal += deptList.get(0).getId().toString() + ",";
                            }
                        }
                        originalVal = newVal;
                    }
                    originalVal = StringUtils.removeEnd(originalVal, ",");
                    break;
                case EXTEND_DEPARTMENT:
                    originalVal = val;
                    if (type.equals(FormFieldValType.SHOW_NAME)) {
                        List<OrgUnit> deptList = OrgDataUtils.findDepartmentByName(null, val);
                        if (Strings.isNotEmpty(deptList)) {
                            originalVal = deptList.get(0).getId().toString();
                        }
                    } else if (type.equals(FormFieldValType.SHOW_CODE)) {
                        List<OrgUnit> deptList = OrgDataUtils.findDepartmentByCode(null, val);
                        if (Strings.isNotEmpty(deptList)) {
                            originalVal = deptList.get(0).getId().toString();
                        }
                    }
                    break;
                case EXTEND_MULTI_DEPARTMENT:
                    originalVal = val;
                    if (type.equals(FormFieldValType.SHOW_NAME)) {
                        String newVal = "";
                        for (String name : val.split(",")) {
                            List<OrgUnit> deptList = OrgDataUtils.findDepartmentByName(null, name);
                            if (Strings.isNotEmpty(deptList)) {
                                newVal += deptList.get(0).getId().toString() + ",";
                            }
                        }
                        originalVal = newVal;
                    } else if (type.equals(FormFieldValType.SHOW_CODE)) {
                        String newVal = "";
                        for (String code : val.split(",")) {
                            List<OrgUnit> deptList = OrgDataUtils.findDepartmentByCode(null, code);
                            if (Strings.isNotEmpty(deptList)) {
                                newVal += deptList.get(0).getId().toString() + ",";
                            }
                        }
                        originalVal = newVal;
                    }
                    originalVal = StringUtils.removeEnd(originalVal, ",");
                    break;
                case EXTEND_POST:
                    originalVal = val;
                    if (type.equals(FormFieldValType.SHOW_NAME)) {
                        List<OrgPost> postList = OrgDataUtils.findOrgPostByName(null, val);
                        if (Strings.isNotEmpty(postList)) {
                            originalVal = postList.get(0).getId().toString();
                        }
                    } else if (type.equals(FormFieldValType.SHOW_CODE)) {
                        List<OrgPost> postList = OrgDataUtils.findOrgPostByCode(null, val);
                        if (Strings.isNotEmpty(postList)) {
                            originalVal = postList.get(0).getId().toString();
                        }
                    }
                    break;
                case EXTEND_MULTI_POST:
                    originalVal = val;
                    if (type.equals(FormFieldValType.SHOW_NAME)) {
                        String newVal = "";
                        for (String name : val.split(",")) {
                            List<OrgPost> postList = OrgDataUtils.findOrgPostByName(null, name);
                            if (Strings.isNotEmpty(postList)) {
                                newVal += postList.get(0).getId().toString() + ",";
                            }
                        }
                        originalVal = newVal;
                    } else if (type.equals(FormFieldValType.SHOW_CODE)) {
                        String newVal = "";
                        for (String code : val.split(",")) {
                            List<OrgPost> postList = OrgDataUtils.findOrgPostByCode(null, code);
                            if (Strings.isNotEmpty(postList)) {
                                newVal += postList.get(0).getId().toString() + ",";
                            }
                        }
                        originalVal = newVal;
                    }
                    originalVal = StringUtils.removeEnd(originalVal, ",");
                    break;
                case EXTEND_LEVEL:
                    originalVal = val;
                    if (type.equals(FormFieldValType.SHOW_NAME)) {
                        List<OrgLevel> levelList = OrgDataUtils.findOrgLevelByName(null, val);
                        if (Strings.isNotEmpty(levelList)) {
                            originalVal = levelList.get(0).getId().toString();
                        }
                    } else if (type.equals(FormFieldValType.SHOW_CODE)) {
                        List<OrgLevel> levelList = OrgDataUtils.findOrgLevelByCode(null, val);
                        if (Strings.isNotEmpty(levelList)) {
                            originalVal = levelList.get(0).getId().toString();
                        }
                    }
                    break;
                case EXTEND_MULTI_LEVEL:
                    originalVal = val;
                    if (type.equals(FormFieldValType.SHOW_NAME)) {
                        String newVal = "";
                        for (String name : val.split(",")) {
                            List<OrgLevel> levelList = OrgDataUtils.findOrgLevelByName(null, name);
                            if (Strings.isNotEmpty(levelList)) {
                                newVal += levelList.get(0).getId().toString() + ",";
                            }
                        }
                        originalVal = newVal;
                    } else if (type.equals(FormFieldValType.SHOW_CODE)) {
                        String newVal = "";
                        for (String code : val.split(",")) {
                            List<OrgLevel> levelList = OrgDataUtils.findOrgLevelByCode(null, code);
                            if (Strings.isNotEmpty(levelList)) {
                                newVal += levelList.get(0).getId().toString() + ",";
                            }
                        }
                        originalVal = newVal;
                    }
                    originalVal = StringUtils.removeEnd(originalVal, ",");
                    break;
                default:
                    originalVal = val;
            }
        }
        return originalVal;
    }


    /**
     * 根据表单的显示名称获取字段的值
     *
     * @param bean
     * @param disPlay
     * @return
     */
    public static Object getFieldValue(FormDataBean bean, String disPlay) {
        if (bean == null) {
            return null;
        }
        FormTableBean table = bean.getFormTable();
        if (table == null) {
            return null;
        }
        FormFieldBean field = table.getFieldBeanByDisplay(disPlay);
        if (field == null) {
            return null;
        }
        Object fieldValue = bean.getFieldValue(field.getName());
        if (fieldValue != null && !"null".equals(fieldValue.toString())) {
            return fieldValue;
        } else {
            return null;
        }
    }

    public static FormFieldBean getFieldBean(FormDataBean bean, String disPlay) {
        if (bean == null) {
            return null;
        }
        FormTableBean table = bean.getFormTable();
        if (table == null) {
            return null;
        }
        return table.getFieldBeanByDisplay(disPlay);
    }


    /**
     * @Description: 表单数据值的类型
     * @Author: CheungHowrAn
     * @Date: 2023-4-24
     * @Param:
     * @return:
     */
    public enum FormFieldValType {
        /**
         * ID
         */
        ID(0),
        /**
         * 登录名
         */
        LOGIN_NAME(1),
        /**
         * 显示值
         */
        SHOW_NAME(2),
        /**
         * 编码
         */
        SHOW_CODE(3),
        /**
         * 枚举序号编码
         */
        SHOW_SORT(4);
        /**
         * KEY
         */
        private int key;

        /**
         * @param key
         */
        FormFieldValType(int key) {
            this.key = key;
        }

        /**
         * @return
         */
        public int getKey() {
            return this.key;
        }

        public int key() {
            return this.key;
        }
    }

}
